package com.ruoyi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.domain.DeviceRestart;
import com.ruoyi.utils.SimpleResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.AdvInoutstock;
import com.ruoyi.service.IAdvInoutstockService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * Controller
 *
 * @author ruoyi
 * @date 2024-05-13
 */
@RestController
@RequestMapping("/system/inoutstock")
public class AdvInoutstockController extends BaseController
{
    @Autowired
    private IAdvInoutstockService advInoutstockService;

    /**
     * 查询列表
     */

    @PreAuthorize("@ss.hasPermi('system:inoutstock:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdvInoutstock advInoutstock)
    {
        startPage();
        List<AdvInoutstock> list = advInoutstockService.selectAdvInoutstockList(advInoutstock);
        return getDataTable(list);
    }

    /**
     * 导出列表
     */

    @PreAuthorize("@ss.hasPermi('system:inoutstock:export')")
    @Log(title = "", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdvInoutstock advInoutstock)
    {
        List<AdvInoutstock> list = advInoutstockService.selectAdvInoutstockList(advInoutstock);
        ExcelUtil<AdvInoutstock> util = new ExcelUtil<AdvInoutstock>(AdvInoutstock.class);
        util.exportExcel(response, list, "数据");
    }

    /**
     * 获取详细信息
     */

    @PreAuthorize("@ss.hasPermi('system:inoutstock:query')")
    @GetMapping(value = "/{advInoutstock}")
    public AjaxResult getInfo(@PathVariable("advInoutstock") Long advInoutstock)
    {
        return success(advInoutstockService.selectAdvInoutstockByAdvInoutstock(advInoutstock));
    }

    /**
     * 新增
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('system:inoutstock:add')")
    @Log(title = "", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdvInoutstock advInoutstock)
    {
        System.out.println("新增--->"+advInoutstock);
        return toAjax(advInoutstockService.insertAdvInoutstock(advInoutstock));
    }

    /**
     * 修改
     */

    @PreAuthorize("@ss.hasPermi('system:inoutstock:edit')")
    @Log(title = "", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdvInoutstock advInoutstock)
    {
        return toAjax(advInoutstockService.updateAdvInoutstock(advInoutstock));
    }

    /**
     * 删除
     */

    @PreAuthorize("@ss.hasPermi('system:inoutstock:remove')")
    @Log(title = "", businessType = BusinessType.DELETE)
	@DeleteMapping("/{advInoutstocks}")
    public AjaxResult remove(@PathVariable Long[] advInoutstocks)
    {
        return toAjax(advInoutstockService.deleteAdvInoutstockByAdvInoutstocks(advInoutstocks));
    }


    @GetMapping("/getdeviceno/{advMembersId}")
    public SimpleResult getDeviceno(@PathVariable("advMembersId") Long advMembersId) {
        List<AdvInoutstock> result = advInoutstockService.getDeviceno(advMembersId);
        return SimpleResult.success(result, "获取成功");
    }


    /**
     * 修改设备重启时间
     * @param advInoutstock
     * @return
     */

    @PostMapping("/updateByRestart")
    public AjaxResult updateByRestart(@RequestBody AdvInoutstock advInoutstock){
        System.out.println("批量修改--->"+advInoutstock);
        return toAjax(advInoutstockService.updateByRestartTime(advInoutstock));
    }

    /**
     * 批量修改设备重启时间
     * @param request
     * @return
     */

    @PostMapping("/batchUpdateReboottime")
    public AjaxResult batchUpdateReboottime(@RequestBody DeviceRestart request) {
        System.out.println(request);
        int rows = advInoutstockService.batchUpdateReboottime(request.getIds(), request.getReboottime());
        return rows > 0 ? AjaxResult.success("批量更新成功") : AjaxResult.error("批量更新失败");
    }

    /**
     * 批量修改设备重启时间
     * @param request
     * @return
     */

    @PostMapping("/batchUpdateUpgrade")
    public AjaxResult batchUpdateUpgrade(@RequestBody DeviceRestart request) {
        System.out.println(request);
        int rows = advInoutstockService.batchUpdateUpgrade(request);
        return rows > 0 ? AjaxResult.success("批量更新成功") : AjaxResult.error("批量更新失败");
    }

    /**
     * 更新设备的声音和亮度大小
     * @return
     */

    @PostMapping("/syncsetting")
    public SimpleResult syncSetting(@RequestBody AdvInoutstock inoutstock) {
        boolean result = advInoutstockService.syncSetting(inoutstock);
        return SimpleResult.success(result?"同步成功":"同步失败");
    }

}
